package org.macpro.alone.server.domain;

import java.io.Serializable;
import java.util.Iterator;
import java.util.List;

/**
 * 
 * @author WangDong
 * @date Mar 14, 2010
 * 
 */
public class SqlDomain implements Serializable {

	private static final long serialVersionUID = 1L;
	private String tableName;// 表名
	private String sortTitle;// 排序字段

	private String sortType;// 排序类型（asc desc）

	private List<LimitDomain> whereList;// 查询条件集合
	public static String initSortTitle( String sorttitle){
		if(sorttitle==null||sorttitle.equals(""))sorttitle="id";
		return sorttitle;
	}
	public static String initsortType( String sorttype){
		if(sorttype==null||sorttype.equals(""))sorttype="asc";
		return sorttype;
	}
	public SqlDomain(String tablename, String sorttitle, String sorttype,
			List<LimitDomain> list) {
		tableName = tablename;
		sortTitle = sorttitle;
		sortType = sorttype;
		whereList = list;
	}

	private String sql_from = "from alone_user";
	private String sql_where = "where alone_user.state='1'";
	private String sql_part;

	private String getSql_from() {
		sql_from = " from " + tableName + " ";
		return sql_from;
	}

	private String getSql_where() {
		if (whereList == null)
			throw new RuntimeException("whereList is null!");
		Iterator<LimitDomain> it_ = whereList.iterator();
		StringBuffer where_ = new StringBuffer("");
		where_.append(" where 1=1 ");
		while (it_.hasNext()) {
			LimitDomain _queryld = it_.next();
			if (_queryld != null)
				where_.append(_queryld.getThisLimit());
		}
		sql_where = where_.toString();
		return sql_where;
	}

	public String getSql_part() {
		sql_part = this.getSql_from() + this.getSql_where()
				+ this.getSortWhere();
		return sql_part;
	}

	private String getSortWhere() {
		if (sortTitle != null && sortType != null && !sortTitle.equals("")
				&& !sortType.equals("")) {
			StringBuffer where = new StringBuffer(" order by ");
			where.append(sortTitle);
			where.append(" ");
			where.append(sortType);
			return where.toString();
		} else {
			return "";
		}
	}

	/**
	 * 一个查询条件的封装
	 * 
	 * @author 王东
	 * 
	 */
	static public class LimitDomain {
		private String key;// where key
		private String value;// where value
		private String comparaSign;// like,}��% = > < >= <=

		public LimitDomain(String key_, String value_, String comparaSign_) {
			key = key_;
			value = value_;
			comparaSign = comparaSign_;
		}

		private String getThisLimit() {
			StringBuffer where_ = new StringBuffer();
			if (key != null && value != null && comparaSign != null) {
				where_.append("  and  ");
				where_.append(key);
				if (comparaSign.equals("like")) {
					where_.append(" ");
					where_.append("like");
				} else {
					where_.append(" ");
					where_.append(comparaSign);
				}
				if (comparaSign.equals("like")) {
					where_.append(" '%");
					where_.append(value);
					where_.append("%'");
				} else {
					where_.append(" '");
					where_.append(value);
					where_.append("'");
				}
				where_.append(" ");
			}
			return where_.toString();
		}

		public String getKey() {
			return key;
		}

		public void setKey(String key) {
			this.key = key;
		}

		public String getValue() {
			return value;
		}

		public void setValue(String value) {
			this.value = value;
		}

		public String getComparaSign() {
			return comparaSign;
		}

		public void setComparaSign(String comparaSign) {
			this.comparaSign = comparaSign;
		}
	}

}
